# -*- coding: utf-8 -*-
"""
PBAR_Discriminant_Leakage.py

Created on Mon May 12 12:49:50 2014

@author: jkwong
"""

from scipy.special import erfc


basepathSet2 = r'C:\Users\jkwong\Documents\Work\PBAR\data4\Mar-files'
# set 2 - load from file
setNum = 2
fullFilename = os.path.join(basepathSet2, 'zspec%dSet.dat' %setNum)
with open(fullFilename ,'rb') as fid:
    print('Reading %s' %fullFilename)
    temp = cPickle.load(fid)

polyMeanOrderExtendIndex = 22
polySigmaOrderExtendIndex = 13

pfitmeanPbALL = temp['pfit']['mean']['PbALL']['extend'][polyMeanOrderExtendIndex]
pfitsigmaPbALL = temp['pfit']['sigma']['PbALL']['extend'][polySigmaOrderExtendIndex]

polyMeanOrderExtendIndex = 22
polySigmaOrderExtendIndex = 13

pfitmeanPbNOT = temp['pfit']['mean']['PbNOT']['extend'][polyMeanOrderExtendIndex]
pfitsigmaPbNOT = temp['pfit']['sigma']['PbNOT']['extend'][polySigmaOrderExtendIndex]


xx = np.arange(400)
ydiff = np.polyval(pfitmeanPbALL, xx) - np.polyval(pfitmeanPbNOT, xx)
ysigma = np.polyval(pfitsigmaPbNOT, xx)

leakage = erfc(ydiff/ysigma)

plt.figure()
plt.grid()

cutt = ydiff > 0
plt.plot(xx[cutt], leakage[cutt])
